package com.oocl.scan.store;

import com.oocl.scan.model.Relationship;
import com.oocl.scan.service.SaveService;
import java.util.concurrent.ConcurrentLinkedQueue;

public class RelationshipStore implements Store<Relationship> {

  private ConcurrentLinkedQueue<Relationship> relationshipQueue;

  private volatile static RelationshipStore relationshipStore;

  public RelationshipStore() {
    this.relationshipQueue = new ConcurrentLinkedQueue<>();
  }

  public static RelationshipStore getInstance() {
    if (relationshipStore == null) {
      synchronized (RelationshipStore.class) {
        if (relationshipStore == null) {
          relationshipStore = new RelationshipStore();
        }
      }
    }
    return relationshipStore;
  }

  @Override
  public void add(Relationship relationship) {
    this.relationshipQueue.add(relationship);
  }

  @Override
  public Relationship poll() {
    return this.relationshipQueue.poll();
  }

  public ConcurrentLinkedQueue<Relationship> getRelationshipQueue() {
    return relationshipQueue;
  }

  public int getCount() {
    return this.relationshipQueue.size();
  }

  public void saveRelationship() {
    if (RelationshipStore.getInstance().getCount() > 0) {
      new SaveService<Relationship>().saveToDISystem("relations", RelationshipStore.getInstance().getRelationshipQueue());
      System.out.println("Total: " + RelationshipStore.getInstance().getRelationshipQueue().size());
    }
  }

  public void setRelationshipQueue(ConcurrentLinkedQueue<Relationship> relationshipQueue) {
    this.relationshipQueue = relationshipQueue;
  }
}
